package com.shixi.shixitraining.shixitraining.ui.tabmine.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.TabhostBaseFragment;
import com.shixi.shixitraining.shixitraining.event.LoginEvent;
import com.shixi.shixitraining.shixitraining.ui.tabmine.activity.ChanagePasswordActivity;
import com.shixi.shixitraining.shixitraining.ui.tabmine.activity.LoginActivity;
import com.shixi.shixitraining.shixitraining.ui.tabmine.activity.MyCollectActivity;
import com.shixi.shixitraining.shixitraining.ui.tabmine.activity.PayRecoderActivity;
import com.shixi.shixitraining.shixitraining.ui.tabmine.activity.PersonInfoActivity;
import com.shixi.shixitraining.shixitraining.util.SharePreferenceUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by shenyan on 2018/7/5.
 */

public class MineFragment extends TabhostBaseFragment implements View.OnClickListener {

    private TextView mUser;
    private TextView mLogin;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_mine;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().isRegistered(this);
        }
    }

    @Override
    protected void initPager() {
        ImageView mUserImg = (ImageView) mContentView.findViewById(R.id.user_img);
        TextView mCollect = (TextView) mContentView.findViewById(R.id.mine_collect_tv);
        TextView mPay = (TextView) mContentView.findViewById(R.id.mine_pay_tv);
        TextView mInfo = (TextView) mContentView.findViewById(R.id.mine_info_tv);
        TextView mSetting = (TextView) mContentView.findViewById(R.id.mine_change_ps_tv);
        mLogin = (TextView) mContentView.findViewById(R.id.mine_login);
//        mUser = (TextView) mContentView.findViewById(R.id.mine_login);
        TextView mSignOut = (TextView) mContentView.findViewById(R.id.tv_sign_out);

        mCollect.setOnClickListener(this);
        mPay.setOnClickListener(this);
        mInfo.setOnClickListener(this);
        mSetting.setOnClickListener(this);
        mLogin.setOnClickListener(this);
        mSignOut.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.mine_collect_tv:
                startActivity(new Intent(getContext(), MyCollectActivity.class));
                break;
            case R.id.mine_pay_tv:
                startActivity(new Intent(getContext(), PayRecoderActivity.class));
                break;
            case R.id.mine_info_tv:
                startActivity(new Intent(getContext(), PersonInfoActivity.class));
                break;
            case R.id.mine_change_ps_tv:
                startActivity(new Intent(getContext(), ChanagePasswordActivity.class));
                break;
            case R.id.mine_login:

                if (!UserManager.getInstance().isUserLogged()){
                    startActivity(new Intent(getContext(), LoginActivity.class));
                } else {
                    mLogin.setText(UserManager.getInstance().getUserName());
                }
                break;
            case R.id.tv_sign_out:
                if (!UserManager.getInstance().isUserLogged()){
                    ToastUtil.showShortToast(getContext(),"您未登录");
                }else {
                    SharePreferenceUtil.clear(SharePreferenceUtil.SPFILENAME.FILE_NAME);
                    mLogin.setText("登录/注册");
                }
                break;
        }
    }

    @Subscribe
    public  void onEventMainThread(LoginEvent event){
        if (event!=null){
            if (!TextUtils.isEmpty(event.getName())){
              mLogin.setText(event.getName());
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
